<?php
namespace app\services\ai\traits\baidu;

trait Voice
{
    /**
     * 语音识别文本
     * @param array $options
     * @return mixed
     */
    public function audio2Text(array $options)
    {
        return $this->call(self::API_SERVER_API, [
            'format' => $options['format'] ?? 'amr', //pcm 或者 wav 或者 amr
            'rate' => $options['rate'] ?? 16000,
            'channel' => $options['channel'] ?? 1,
            'speech' => $options['speech'] ?? '',
            'cuid' => str_random(60),
            'token' => $this->accessToken,
            'dev_pid' => $options['dev_pid'] ?? 1537,
            'len' => $options['len'] ?? 0,
        ], self::JSON);
    }

    /**
     * 文本合成语音
     * @param array $options
     * @return mixed
     */
    public function text2Audio(array $options)
    {
        return $this->call(self::API_TEXT_2_AUDIO, [
            'tex' => $options['tex'] ?? '百度语音合成',
            'tok' => $this->accessToken,
            'cuid' => str_random(60),
            'ctp' => strval($options['ctp'] ?? '1'),
            'lan' => $options['lan'] ?? 'zh',
            'spd' => strval($options['spd'] ?? '5'),
            'pit' => strval($options['pit'] ?? '5'),
            'vol' => strval($options['vol'] ?? '5'),
            'per' => strval($options['per'] ?? '0'), //0为普通女声，1为普通男生，3为情感合成-度逍遥，4为情感合成-度丫丫，默认为普通女声
            'aue' => strval($options['aue'] ?? '3'), //3为mp3格式(默认)； 4为pcm-16k；5为pcm-8k；6为wav
        ]);
    }
}
